package com.csu.mypetstore.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.mypetstore.api.common.CommonResponse;
import com.csu.mypetstore.api.entity.*;
import com.csu.mypetstore.api.persistence.AccountMapper;
import com.csu.mypetstore.api.persistence.BannerDataMapper;
import com.csu.mypetstore.api.persistence.ProfileMapper;
import com.csu.mypetstore.api.persistence.SignOnMapper;
import com.csu.mypetstore.api.service.AccountService;
import com.csu.mypetstore.api.vo.AccountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private BannerDataMapper bannerDataMapper;
    @Autowired
    private ProfileMapper profileMapper;
    @Autowired
    private SignOnMapper signOnMapper;


    @Override
    public CommonResponse<AccountVO> getAccount(String username, String password) {
        QueryWrapper<SignOn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        //password=encode(password);//加密
        queryWrapper.eq("password", password);

        SignOn signOn = signOnMapper.selectOne(queryWrapper);

        if (signOn == null) {
            return CommonResponse.createForError("用户名或密码不正确");
        }
        return getAccount(username);
    }

    @Override
    public CommonResponse<AccountVO> getAccount(String username) {
        Account account = accountMapper.selectById(username);
        Profile profile = profileMapper.selectById(username);
        BannerData bannerData = bannerDataMapper.selectById(profile.getFavouriteCategoryId());

        if (account == null) {
            return CommonResponse.createForError("获取用户信息失败");
        }
        AccountVO accountVO = entityToVO(account, profile, bannerData);

        return CommonResponse.createForSuccess(accountVO);
    }

    @Override
    public CommonResponse<AccountVO> registerAccount(AccountVO accountVO) {
        SignOn signOn = VOToSignOn(accountVO);
        signOn.setPassword(encode(signOn.getPassword()));
        Account account = VOToAccount(accountVO);
        Profile profile =VOToProfile(accountVO);

        int a= signOnMapper.insert(signOn);
        int b =accountMapper.insert(account);
        int c =profileMapper.insert(profile);

        if (a+b+c==3){
            return CommonResponse.createForSuccess(accountVO);
        }
        return CommonResponse.createForError("插入出错");
    }

    @Override
    public CommonResponse<Boolean> isUsernameExist(String username){
        if (signOnMapper.selectById(username)!=null){//存在
            return  CommonResponse.createForSuccess(true);
        }
        return CommonResponse.createForSuccess(false);
    }

    @Override
    public CommonResponse<Integer> resetPassword(SignOn account) {
//        account.setPassword(DigestUtils.md5DigestAsHex(account.getPassword().getBytes()));
        account.setPassword(encode(account.getPassword()));
        account.setPassword(account.getPassword());
        int b= signOnMapper.updateById(account);
        if (b==1)
            return CommonResponse.createForSuccess(1);
        else
            return CommonResponse.createForError("重置密码失败");
    }

    @Override
    public CommonResponse<AccountVO> updateAccountInfo(AccountVO accountVO) {
        int a=accountMapper.updateById(VOToAccount(accountVO));
        int b=profileMapper.updateById(VOToProfile(accountVO));
        if(a+b==2)
            return CommonResponse.createForSuccess(accountVO);
        else
            return CommonResponse.createForError("修改出错");
    }


    private AccountVO entityToVO(Account account, Profile profile, BannerData bannerData) {
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(account.getUsername());
        accountVO.setPassword("");
        accountVO.setEmail(account.getEmail());
        accountVO.setFirstName(account.getFirstName());
        accountVO.setLastName(account.getLastName());
        accountVO.setStatus(account.getStatus());
        accountVO.setAddress1(account.getAddress1());
        accountVO.setAddress2(account.getAddress2());
        accountVO.setCity(account.getCity());
        accountVO.setState(account.getState());
        accountVO.setCountry(account.getCountry());
        accountVO.setZip(account.getZip());
        accountVO.setPhone(account.getPhone());

        accountVO.setLanguagePreference(profile.getLanguagePreference());
        accountVO.setBannerOption(profile.getBannerOption());
        accountVO.setListOption(profile.getListOption());
        if (profile.getBannerOption()) {
            accountVO.setFavouriteCategoryId(profile.getFavouriteCategoryId());
            accountVO.setBannerName(bannerData.getBannerName());
        } else {
            accountVO.setFavouriteCategoryId("");
            accountVO.setBannerName("");
        }
        return accountVO;
    }


    private Account VOToAccount(AccountVO accountVO){
        Account account = new Account();
        account.setUsername(accountVO.getUsername());
        account.setEmail(accountVO.getEmail());
        account.setFirstName(accountVO.getFirstName());
        account.setLastName(accountVO.getLastName());
        account.setStatus(accountVO.getStatus());
        account.setAddress1(accountVO.getAddress1());
        account.setAddress2(accountVO.getAddress2());
        account.setCity(accountVO.getCity());
        account.setState(accountVO.getState());
        account.setCountry(accountVO.getCountry());
        account.setZip(accountVO.getZip());
        account.setPhone(accountVO.getPhone());
        return account;
    }

    private Profile VOToProfile(AccountVO accountVO){
        Profile profile = new Profile();
        profile.setUsername(accountVO.getUsername());
        if (accountVO.getLanguagePreference()!=null)
            profile.setLanguagePreference(accountVO.getLanguagePreference());
        profile.setBannerOption(accountVO.isBannerOption());
        profile.setListOption(accountVO.isListOption());

        profile.setFavouriteCategoryId(accountVO.getFavouriteCategoryId());
        return profile;
    }

    private SignOn VOToSignOn(AccountVO accountVO){
        SignOn signOn = new SignOn();
        signOn.setUsername(accountVO.getUsername());
//        signOn.setPassword(encode(accountVO.getPassword()));
        signOn.setPassword(accountVO.getPassword());
        return signOn;
    }

    //加密
    private String encode(String password){
        return  DigestUtils.md5DigestAsHex(password.getBytes());
    }
}


















